package com.tms.app.tms.utils;

import android.content.Context;
import android.os.CountDownTimer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by hl on 2017/5/24.
 */

public class TimeUtil {
    public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DEFAULT_DATE_FORMAT2 = new SimpleDateFormat("yyyyMMddHHmmss");
    public static final SimpleDateFormat DATE_FORMAT_DATE    = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat DATE_FORMAT_DATE_1    = new SimpleDateFormat("yyyy/MM/dd");
    public static final SimpleDateFormat DATE_FORMAT_DATE_YEAR_M    = new SimpleDateFormat("yyyy-MM");
    public static final SimpleDateFormat DATE_FORMAT_DATE_YEAR_M_DOT    = new SimpleDateFormat("yyyy.MM");
    public static final SimpleDateFormat DATE_FORMAT_DATE_YEAR_M__D_DOT    = new SimpleDateFormat("yyyy.MM.dd");

    public static String getTimeYMD(Date date)
    {
        return DATE_FORMAT_DATE.format(date);
    }
    public static String getTime(Date date)
    {
        return DATE_FORMAT_DATE_YEAR_M.format(date);
    }
    public static String getTime(Date date, DateFormat format)
    {
        return format.format(date);
    }
    public static String getTime(long time, DateFormat format)
    {
        return format.format(new Date(time));
    }

    public static Long getTimeFormat(){
        String time=DEFAULT_DATE_FORMAT2.format(new Date());
        return Long.parseLong(time);
    }
    public static String getTimeFormat(Long time){
        return getTimeFormat(time+"");
    }
    public static String getTimeFormat(String time){
        if (time==null)
            return "";
        time=time.replace(" ","");
        StringBuffer buffer=new StringBuffer();
        if (time.length()==6){
            buffer.append(time.substring(0,2))
                    .append("-")
                    .append(time.substring(2,4))
                    .append("-")
                    .append(time.substring(4,6));
            return buffer.toString();
        }
        if (time.length()==8){
            buffer.append(time.substring(2,4))
            .append("-")
                    .append(time.substring(4,6))
                    .append("-")
                    .append(time.substring(6,8));
            return buffer.toString();
        }
        if (time.length()==12){
            buffer.append(time.substring(2,4))
                    .append("-")
                    .append(time.substring(4,6))
                    .append("-")
                    .append(time.substring(6,8))
                    .append(" ")
                    .append(time.substring(8,10))
                    .append(":")
                    .append(time.substring(10,12));
            return buffer.toString();
        }

        if (time.length()>=14){
            buffer.append(time.substring(2,4))
                    .append("-")
                    .append(time.substring(4,6))
                    .append("-")
                    .append(time.substring(6,8))
                    .append(" ")
                    .append(time.substring(8,10))
                    .append(":")
                    .append(time.substring(10,12))
                    .append(":")
                    .append(time.substring(12,14));
            return buffer.toString();
        }

        return buffer.toString();
    }


    public static int getInt(long time,int field){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date(time));
        if (field==Calendar.MONTH){
            return calendar.get(field)+1;
        }
        return calendar.get(field);
    }

    public static String getNewDeteTime(Context context){
        String msgTime=SharePreferenceUtil.getString(context,"msgTime","");
        if (msgTime.equals("")){
            msgTime=DEFAULT_DATE_FORMAT2.format(new Date());
        }
     return msgTime;
    }



    /******
     * 计时器
     * @param time  计时总长
     * @param ctime  间隔
     * @param event  回调
     */
    public static void timeDown(final int time, int ctime, final TimeDownEvent event){
        final int dd=ctime;
        CountDownTimer cdt = new CountDownTimer(time, ctime) {
            int nctime=0;
            @Override
            public void onTick(long millisUntilFinished) {
                nctime+=dd;
               event.timeDown(nctime);
            }
            @Override
            public void onFinish() {
                event.timeDown(-1);
            }
        };
        cdt.start();
    }
    public static void timeDown(final int ctime, final TimeDownEvent event){
        timeDown(ctime,1000,event);
    }


    public interface  TimeDownEvent{
        /******
         *
         * @param indexTime  已运行时间
         * @param  //剩余时间
         */
        void timeDown(int indexTime);
    }

}
